package com.creative.xcx.controller;

import com.alibaba.excel.EasyExcel;
import com.creative.core.controller.BaseController;
import com.creative.core.domain.AjaxResult;
import com.creative.core.page.TableDataInfo;
import com.creative.xcx.domain.XcxNotice;
import com.creative.xcx.service.XcxNoticeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author ling
 * @description 小程序的公告栏
 * @date 2025-02-26
 */
@RestController
@RequestMapping(value = "/XcxNotice")
public class XcxNoticeController extends BaseController {

    @Resource
    private XcxNoticeService xcxNoticeService;

    /**
     * 新增
     *
     * @author ling
     * @date 2025/02/26
     **/
    @PostMapping
    public AjaxResult insert(@RequestBody XcxNotice xcxNotice) {
        return toAjax(xcxNoticeService.insert(xcxNotice));
    }

    /**
     * 刪除单个
     *
     * @author ling
     * @date 2025/02/26
     **/
    @DeleteMapping("/{nId}")
    public AjaxResult delete(@PathVariable("nId") String nId) {
        return toAjax(xcxNoticeService.delete(nId));
    }

    /**
     * 批量刪除
     *
     * @author ling
     * @date 2025/02/26
     **/

    @DeleteMapping("/batch")
    public AjaxResult deleteBatch(@RequestBody String[] ids) {
        return toAjax(xcxNoticeService.deleteBatch(ids));
    }

    /**
     * 更新
     *
     * @author ling
     * @date 2025/02/26
     **/
    @PutMapping("/update")
    public AjaxResult update(@RequestBody XcxNotice xcxNotice) {
        return toAjax(xcxNoticeService.update(xcxNotice));
    }


    /**
     * 查询 根据主键 id 查询
     *
     * @author ling
     * @date 2025/02/26
     **/
    @GetMapping(value = "/{nId}")
    public AjaxResult selectById(@PathVariable("nId") String nId) {

        if (nId == null) {
            return error("参数为空");
        }
        return success(xcxNoticeService.selectById(nId.trim()));
    }

    /**
     * 查询 分页查询(带条件)
     *
     * @author ling
     * @date 2025/02/26
     **/
    @GetMapping("/list")
    public TableDataInfo selectXcxNoticeList(XcxNotice xcxNotice) {

        return xcxNoticeService.selectXcxNoticeList(xcxNotice);
    }

    /**
     * 获取小程序公告栏数据
     *
     * @param xcxNotice 对象
     * @return {@link TableDataInfo }
     * @author ling
     * @date 2025/02/26
     */
    @GetMapping("/mp/list")
    public AjaxResult selectMpXcxNoticeList(XcxNotice xcxNotice) {
        return success(xcxNoticeService.selectMpXcxNoticeList(xcxNotice));
    }

    /**
     * 批量导出
     *
     * @author ling
     * @date 2025/02/26
     **/
    @PostMapping("/export")
    public void export(@RequestBody String[] ids, HttpServletResponse response) throws IOException {
        //        判断ids是否为空
        if (ids == null || ids.length == 0) {
            // ids 为空或未初始化时执行的代码
            throw new RuntimeException("ids为空");
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("小程序的公告栏" + System.currentTimeMillis(), StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), XcxNotice.class)
                .sheet("小程序的公告栏")
                .doWrite(() -> {
                    // 查询数据
                    return xcxNoticeService.exportBatch(ids);
                });
        logger.info("导出成功");
    }
}
